package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.constant.OperaPswConstant;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.Machine;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.base.MachineService;
import com.tbit.uqbike.webmanager.util.HttpClientUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jing
 * @date 2020/12/710:31
 * @Description:
 */
@RestController
@RequestMapping("/losebattery")
public class LoseBatteryController {
    @Resource
    private MachineService machineService;

    @PostMapping("/add")
    public JsonResult add(String userCode) {
        String url = OperaPswConstant.ACCOUNT_OPERA_PUSH_URL + "/machine/addBattery.do";
        Machine machine = machineService.getByUserCode(userCode);
        if (null == machine) {
            throw new BaseException(SystemCodeEnum.MACHINE_NOT_EXIST);
        }
        NameValuePair pair = new BasicNameValuePair("userCode", userCode);
        List<NameValuePair> list = new ArrayList<>();
        list.add(pair);
        String result = HttpClientUtils.httpPost(url, null, list);
        return JsonResult.ok(result);
    }

}
